import Dysmsapi20170525 from '@alicloud/dysmsapi20170525';
import OpenApi from '@alicloud/openapi-client';
import Util from '@alicloud/tea-util';

function createClient() {
    let config = new OpenApi.Config({
      accessKeyId: 'LTAI5tHKqTN4oAr6Tt996HL9',
      accessKeySecret: 'r30BrqkjXrzHQyFt4fQdiOMQf1PCLd',
    });
    config.endpoint = `dysmsapi.aliyuncs.com`;
    return new Dysmsapi20170525.default(config);
  }

  async  function sendSms(phone,code){
    let client = createClient();
    let sendSmsRequest = new Dysmsapi20170525.SendSmsRequest({
      signName: '云南坤岑网络技术',
      templateCode: 'SMS_305580884',
      phoneNumbers: phone,
      templateParam: `{"code":"${code}"}`,
    });
    let runtime = new Util.RuntimeOptions({});
    try {
      // 复制代码运行请自行打印 API 的返回值
     let res = await client.sendSmsWithOptions(sendSmsRequest, runtime);
     console.log(JSON.stringify(res.body));
     return true
    } catch (error) {
      // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
      // 错误 message
      console.log(error.message);
      // 诊断地址
      console.log(error.data["Recommend"]);
      Util.default.assertAsString(error.message);
    }
    return false;
  }
    export {sendSms}

